package com.ebelter.bodyfatscale.util;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RadioGroup;

import com.ebelter.bodyfatscale.R;

/**
 * Created by ZCQ on 2017/11/29
 */

public class ViewUtils {


    public static void recyclerViewScrollToPosition(RecyclerView recyclerView, int n) {
        if (recyclerView==null){
            return;
        }
        int count=recyclerView.getAdapter().getItemCount();
        if (count==0){
            return;
        }
        if (n>=count){
            n=count-1;
        }else if (n<0){
            n=0;
        }
        recyclerView.scrollToPosition(n);
    }

    /**
     * 让ViewGround和它的子View全部选中或者不选中
     */
    public static void childSelected(ViewGroup viewGroup, boolean selected) {
        if (viewGroup != null) {
            viewGroup.setSelected(selected);
            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                View childView = viewGroup.getChildAt(i);
                if (childView != null) childView.setSelected(selected);
            }
        }

    }


    public static void disableRadioGroup(RadioGroup testRadioGroup) {
        for (int i = 0; i < testRadioGroup.getChildCount(); i++) {
            testRadioGroup.getChildAt(i).setEnabled(false);
        }
    }

    public static void setRadioGroupAllAlpha(RadioGroup testRadioGroup,float alpha) {
        for (int i = 0; i < testRadioGroup.getChildCount(); i++) {
            testRadioGroup.getChildAt(i).setAlpha(alpha);
        }
    }

    public static void enableRadioGroup(RadioGroup testRadioGroup) {
        for (int i = 0; i < testRadioGroup.getChildCount(); i++) {
            testRadioGroup.getChildAt(i).setEnabled(true);
        }
    }

    private static Animation shake;
    public static void shakeView(View view){
        if (view==null)return;
        if (shake==null){
            shake = AnimationUtils.loadAnimation(view.getContext().getApplicationContext(), R.anim.shake);//加载动画资源文件
        }
        view.startAnimation(shake);

    }

    /**
     * 隐藏控件
     */
    public static void hideView(View view) {
        if (view != null && view.getVisibility() != View.INVISIBLE) {
            view.setVisibility(View.INVISIBLE);
        }
    }

    /**
     * gone控件
     */
    public static void goneView(View view) {
        if (view != null && view.getVisibility() != View.GONE) {
            view.setVisibility(View.GONE);
        }
    }

    public static void setAlpha(View view,float alpha) {
        if (view != null) {
            view.setAlpha(alpha);
        }
    }

    /**
     * 显示控件
     */
    public static void displayView(View view) {
        if (view != null && view.getVisibility() != View.VISIBLE) {
            view.setVisibility(View.VISIBLE);
        }
    }

    /**设置权重*/
    public static void setWeight(View view ,float weight){
        ViewGroup.LayoutParams lp = view.getLayoutParams();
        if (lp!=null&&lp instanceof LinearLayout.LayoutParams){
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
            layoutParams.weight=weight;
            view.setLayoutParams(layoutParams);
        }

    }





    public static int getMeasuredHeight(View view){
        if (view==null){
            return 0;
        }
        return view.getMeasuredHeight();
    }

    public static int getMeasuredWidth(View view){
        if (view==null){
            return 0;
        }
        return view.getMeasuredWidth();
    }



    public static void setViewWidthHeight(View view,int width,int height){
        ViewGroup.LayoutParams params = view.getLayoutParams();
        params.width = width;
        params.height = height;
        view.setLayoutParams(params);
    }
    public static void setViewHeight(View view,int height){
        if (view==null){
            return;
        }
        ViewGroup.LayoutParams params = view.getLayoutParams();
        if (params==null){
            params=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        params.height = height;
        view.setLayoutParams(params);
    }

    public static void setViewHeight2(View view,int height){
        if (view==null){
            return;
        }
        ViewGroup.LayoutParams params = view.getLayoutParams();
        if (params==null){
            params=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        params.height = height;
        view.setLayoutParams(params);
    }
    public static void setViewWidthHeightSame(View view,int width){
        setViewWidthHeight(view,width,width);
    }


    public static void setListViewHeightBasedOnChildren(ListView listView) {
        //获取ListView对应的Adapter
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = 0;
        for (int i = 0, len = listAdapter.getCount(); i < len; i++) {   //listAdapter.getCount()返回数据项的数目
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);  //计算子项View 的宽高
            totalHeight += listItem.getMeasuredHeight();  //统计所有子项的总高度
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        //listView.getDividerHeight()获取子项间分隔符占用的高度
        //params.height最后得到整个ListView完整显示需要的高度
        listView.setLayoutParams(params);
    }


    public static void setImageResource(ImageView imageView,int res){
        if (imageView!=null){
            imageView.setImageResource(res);
        }
    }
}
